package com.zw.yzk.kabu.view.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.internal.component.DaggerAddShippingAddressComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.AddShippingAddressModule;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.management.ToolbarBuilder;
import com.zw.yzk.kabu.model.DistrictModel;
import com.zw.yzk.kabu.presenter.AddShippingAddressPresenter;
import com.zw.yzk.kabu.utils.CityUtils;
import com.zw.yzk.kabu.view.view.AddShippingAddressView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by zhanwei on 2017/1/11.
 */

public class AddShippingAddressActivity extends BaseActivity implements AddShippingAddressView {

    @Bind(R.id.consignee)
    EditText consignee;
    @Bind(R.id.contact_info)
    EditText contactInfo;
    @Bind(R.id.area)
    TextView area;
    @Bind(R.id.location)
    EditText location;

    @Inject
    AddShippingAddressPresenter presenter;
    private List<String> provinceList;
    private List<List<String>> cityList;
    private String province;
    private String city;

    @Override
    public int getLayoutId() {
        return R.layout.activity_add_shiiping_address;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {
        ToolbarBuilder
                .createBuilder(this)
                .setTitle(getResources().getString(R.string.create_shipping_address))
                .build();
    }

    @Override
    public void initComponent() {
        DaggerAddShippingAddressComponent.builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .addShippingAddressModule(new AddShippingAddressModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        loadDistrictData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.destroy();
        }
    }

    @Override
    public void addAddressSuccess() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, 1000);
    }

    @OnClick(R.id.layout_area)
    public void selectArea() {
        hideSoftKeyBoard();
        showSelectDistrictDialog();
    }

    @OnClick(R.id.save_and_use)
    public void saveAndUse() {
        String consigneeStr = consignee.getText().toString();
        String contactStr = contactInfo.getText().toString();
        String areaStr = area.getText().toString();
        String locationStr = location.getText().toString();

        if (TextUtils.isEmpty(consigneeStr)) {
            ToastManager.getInstance().showToast(this, getResources().getString(R.string.please_input_consignee));
        } else if (TextUtils.isEmpty(contactStr)) {
            ToastManager.getInstance().showToast(this, getResources().getString(R.string.please_input_contact_info));
        } else if (TextUtils.isEmpty(areaStr)) {
            ToastManager.getInstance().showToast(this, getResources().getString(R.string.please_input_area));
        } else if (TextUtils.isEmpty(locationStr)) {
            ToastManager.getInstance().showToast(this, getResources().getString(R.string.please_input_location));
        } else {
            addShippingAddress(consigneeStr, contactStr, locationStr);
            LoadingDialogManager.getInstance().showLoadingDialog(this);
        }
    }

    /**
     * 设置地区信息
     *
     * @param p 省
     * @param c 城市
     */
    private void setRegion(String p, String c) {
        province = p;
        city = c;
        area.setText(TextUtils.isEmpty(province) ? "" : province + (TextUtils.isEmpty(city) ? "" : city));
    }

    /**
     * 加载地区信息
     */
    private void loadDistrictData() {
        provinceList = new ArrayList<>();
        cityList = new ArrayList<>();
        DistrictModel[] data = CityUtils.getLocalCityList(this);
        if (data == null) {
            return;
        }
        for (DistrictModel model : data) {
            provinceList.add(model.district);
            cityList.add(model.children);
        }
    }

    /**
     * 显示地区选择对话框
     */
    private void showSelectDistrictDialog() {
        if (provinceList.isEmpty()) {
            return;
        }
        OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                setRegion(provinceList.get(options1), cityList.get(options1).get(options2));
            }
        }).build();
        pvOptions.setPicker(provinceList, cityList);
        pvOptions.show();
    }

    /**
     * 添加收货地址
     */
    private void addShippingAddress(String receiver, String phone, String details) {
        presenter.addShippingAddress(receiver, phone, province, city, details);
    }

    /**
     * 隐藏软键盘
     */
    public void hideSoftKeyBoard() {
        try {
            InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
